import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/interlock_removal_ledger_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';

class ChainRemovalListController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? searchX;

  /// 筛选条件列表
  List<FilterBean> filterList = [];

  /// 状态筛选条件
  String? status;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 状态筛选条件
    final List<FilterEntitiesBean> statusList = [];
    statusList.add(FilterEntitiesBean(name: '待发起', value: '1'));
    statusList.add(FilterEntitiesBean(name: '待审批', value: '2'));
    statusList.add(FilterEntitiesBean(name: '审批中', value: '3'));
    statusList.add(FilterEntitiesBean(name: '通过', value: '4'));
    statusList.add(FilterEntitiesBean(name: '驳回', value: '5'));
    //筛选控制bean
    final FilterBean filterBeanStatus = FilterBean(title: '状态', filterList: statusList);
    filterList.add(filterBeanStatus);
  }

  // 获取数据
  Future<List<InterlockRemovalLedgerEntity>> interlockRemovalLedgerList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<InterlockRemovalLedgerEntity>>(
      Interface.interlockRemovalLedgerList,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'name': searchX, //名称
        'auditStatus': status, //状态 1:待发起 2:待审批3：审批中 4：通过 5：驳回
        // 'equipmentId': '', //设备id
      },
    ).execute();
    List<InterlockRemovalLedgerEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    status = '';
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '状态') {
        status = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
